package com.itheima.Test01;

import com.itheima.bean.Course;
import com.itheima.bean.Student;
import com.itheima.manyToMany.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ManyToManyTest {
    @Test
    public void selectManyAll() throws IOException {
        InputStream is = Resources.getResourceAsStream("OneToOne.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectAll();
        for (Student student :students) {
            System.out.println(student.getId()+","+student.getName());
            List<Course> courses = student.getCourses();
            for (Course course:courses){
                System.out.println("\t"+course);
            }
        }
    }
}
